package com.chen.q2;

import com.chen.q2.JdbcInsert.JDBCInsert;
import com.chen.q2.JdbcInsert.MultiThreadInsert;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.concurrent.CountDownLatch;

@SpringBootApplication
@Slf4j
public class Application {

    public static void main(String[] args) {
        ApplicationContext context = SpringApplication.run(Application.class,args);
        /*DataSource dataSource = context.getBean(DataSource.class);
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        try {
            connection = dataSource.getConnection();
            connection.setAutoCommit(false);
            String sql = "INSERT INTO t_user (id,account) VALUES(?,?)";
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setInt(1,11);
            preparedStatement.setString(2,"test");
            preparedStatement.addBatch();
            preparedStatement.setInt(1,12);
            preparedStatement.setString(2,"test");
            preparedStatement.addBatch();
            preparedStatement.executeBatch();
            connection.commit();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                preparedStatement.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            try {
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }*/
        //单线程插入大概60秒
        /*JDBCInsert jdbcInsert = context.getBean(JDBCInsert.class);
        long start = System.currentTimeMillis();
        jdbcInsert.executeOnMillionInsert();
        long end = System.currentTimeMillis();
        log.info("插入100W条订单数据耗时:{}毫秒" ,end - start);*/

        //多线程插入大概15秒
        MultiThreadInsert jdbcInsert = context.getBean(MultiThreadInsert.class);
        long start = System.currentTimeMillis();
        try {
            jdbcInsert.executeOnMillionInsert();
        } catch (SQLException  e) {
            e.printStackTrace();
        }
        long end = System.currentTimeMillis();
        log.info("插入100W条订单数据耗时:{}毫秒" ,end - start);
        System.exit(0);
    }
}
